/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package babydisco.NN;

import babydisco.ECGData;
import babydisco.Math.Wavelet;
import babydisco.util.DataReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.train.MLTrain;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.training.simple.TrainAdaline;
import org.encog.neural.pattern.ADALINEPattern;
import org.encog.persist.EncogDirectoryPersistence;

/**
 * Creates and trains a new neural network for removing (mothers) 
 * signals from maternal ECG 
 * @author fransheuvelmans
 */
public class Trainer
{

    /**
     * Special tester main for neural nets
     * @param args 
     */
    public static void main(String[] args)
    {
        ElmanNetwork bergmans = null;

        boolean newNet = true;

        File file;
        JFileChooser chooser = new JFileChooser();
        int ret;

        if (newNet)
        {
            bergmans = new ElmanNetwork(30);
        }
        else
        {
            ret = chooser.showDialog(null, "Open file");

            if (ret == JFileChooser.APPROVE_OPTION)
            {
                file = chooser.getSelectedFile();

                BasicNetwork bla = (BasicNetwork) EncogDirectoryPersistence.loadObject(new File(file.getAbsolutePath()));
                bergmans = new ElmanNetwork(bla);
            }
            ret = 0;
        }

        ret = chooser.showDialog(null, "Open file");

        if (ret == JFileChooser.APPROVE_OPTION)
        {
            file = chooser.getSelectedFile();
            try
            {
                ArrayList[] data = DataReader.readTrainingData(file.getAbsolutePath());
                
                ArrayList[] useFull = newToOld(data);
                
                //ArrayList input = new ArrayList((Collection) useFull[0]);
                //Collections.copy(useFull[0],input);
                useFull[0] = preprocess(useFull[0]);
                
                MLDataSet trainingdata = ElmanNetwork.convertTrainData(data);
                bergmans.setTraining(trainingdata);
            }
            catch (IOException ex)
            {
                Logger.getLogger(Trainer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        double error = bergmans.trainNetwork(200);

        System.out.println("Oddity: " + error);
        System.out.println(bergmans.giveWeights());

        int returnVal = chooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            File file2 = chooser.getSelectedFile();
            //This is where a real application would save the file.
            EncogDirectoryPersistence.saveObject(new File(file2.getAbsolutePath()), bergmans.network);
        }
        else
        {
            System.out.println("Save command cancelled by user.");
        }
        System.out.println("KLAAARRRRR");
    }
    
    /**
     * Converts the new type, 
     */
    public static ArrayList[] newToOld(ArrayList[] newStyle)
    {
        ArrayList[] oldStyle = new ArrayList[2];
        oldStyle[0] = newStyle[1]; // 2nd row is the combined signal
        oldStyle[1] = newStyle[2]; // 3rd row is baby only (so the ideal)
        
        return oldStyle;
    }
    
    public static ArrayList preprocess(ArrayList dirtyInput)
    {
        ArrayList clean = new ArrayList();
        clean = Wavelet.waveletFilter(dirtyInput);
        
        // more steps can be added here
        return clean;
    }
}
